package com.project.until;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLDecoder;

public class UploadUntil {
    
    public static String getFileName(MultipartFile mf,String dirPath){

        //得到上传文件的文件名
        String fileName = mf.getOriginalFilename();
        //重新命名文件
        fileName = System.currentTimeMillis() + fileName.substring(fileName.lastIndexOf("."));

        //得到上传文件的存放路径（真实路径）
        URL url = Thread.currentThread().getContextClassLoader().getResource(dirPath);

        String filPath = null;
        try {
            filPath = URLDecoder.decode(url.getFile(),"utf-8");
            //把上传文件的二进制数据，写入指定文件中
            mf.transferTo(new File(filPath + "/" + fileName));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return fileName;

    }
}
